<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-22 10:26:19
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-24 13:06:25
 * @Description: 
 * 
-->
<template>
  <el-dialog
    draggable
    overflow
    v-model="visible"
    :title="titleMap[mode]"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
      <sk-tabs type="border-card">
        <el-tab-pane label="承兑信息">
          <el-row :gutter="40">
            <el-col :span="8">
              <el-form-item label="汇票编号" prop="code">
                <el-input v-model="form.code"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开户银行" prop="bank">
                <el-input v-model="form.bank"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="票面金额" prop="amount">
                <el-input v-model="form.amount" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="出票日期" prop="draftdate">
                <el-date-picker
                  v-model="form.draftdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="到期日期" prop="duedate">
                <el-date-picker
                  v-model="form.duedate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收到日期" prop="recedate">
                <el-date-picker
                  v-model="form.recedate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="月利率" prop="rate">
                <el-input v-model="form.rate" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="计息天数" prop="interestdays">
                <el-input v-model="form.interestdays" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="调整天数" prop="rangedays">
                <el-input v-model="form.rangedays" />
              </el-form-item>
            </el-col>

            <el-col :span="4">
              <el-form-item label="贴现利息" prop="interestamount">
                <el-input v-model="form.interestamount" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="贴现金额" prop="discountamount">
                <el-input v-model="form.discountamount" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="内转日期" prop="handletrade_billdate">
                <el-date-picker
                  v-model="form.handletrade_billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收票抬头" prop="trade_id">
                <sk-select-group
                  v-model="form.trade_id"
                  :url="$API.basedata.trade.list"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="库存抬头" prop="handletrade_id">
                <sk-select-group
                  v-model="form.handletrade_id"
                  :url="$API.basedata.trade.list"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="购票账户" prop="buyaccount_id">
                <sk-select-group
                  v-model="form.buyaccount_id"
                  :list="accountList"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="应收单位" prop="customer_id">
                <skAutoChoose
                  v-model="form.customer.name"
                  :name="'customer'"
                  :assign="form"
                  @select="selectCustomer"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="付款抬头" prop="billtrade_id">
                <sk-select-group
                  v-model="form.billtrade_id"
                  :list="billtradeList"
                ></sk-select-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收兑行" prop="bank_remark">
                <skDicSelect
                  v-model="this.form.bank_remark"
                  :code="'bank_remark'"
                ></skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" rows="3"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-alert type="info" show-icon :closable="false">
                <p><b>客户收:</b>应收单位</p>
                <p>贷：应收账款</p>
                <p>借：应收票据.承兑账号</p>
              </el-alert>
            </el-col>
            <el-col :span="8">
              <el-alert type="info" show-icon :closable="false">
                <p><b>购：</b>购票账号，应收单位</p>
                <p>贷：银行存款</p>
                <p>借：应收票据.承兑账号</p>
              </el-alert>
            </el-col>
            <el-col :span="8">
              <el-alert type="info" show-icon :closable="false">
                <p><b>银行借：</b>无</p>
                <p>贷：承兑借款</p>
                <p>借：应收票据.承兑账号</p>
              </el-alert>
            </el-col>
          </el-row>
        </el-tab-pane>
      </sk-tabs>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skSelectGroup from "@/components/skSelectGroup";
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: { skSelectGroup, skDicSelect, skAutoChoose },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      billtradeList: [],
      accountList: [],
      form: {
        id: "",
        recedate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        rate: 0,
        amount: 0,
        rangedays: 0,
        buyinterest: 0,
        customer: {},
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
        code: { required: true, message: "汇票编号必填" },
        bank: { required: true, message: "开户银行必填" },
        amount: { required: true, message: "票面金额必填" },
        draftdate: { required: true, message: "出票日期必填" },
        duedate: { required: true, message: "到期日期必填" },
        rangedays: { required: true, message: "调整天数必填" },
        trade_id: { required: true, message: "收票抬头必选" },
      },
      visible: false,
      isSaveing: false,
    };
  },

  mounted() {
    this.getAccount();
  },
  watch: {
    "form.amount"() {
      this.computeInterestendamount();
    },
    "form.trade_id"() {
      this.form.handletrade_id = this.form.trade_id;
    },
    "form.rate"() {
      this.computeInterestendamount();
    },
    "form.duedate"() {
      this.computeInterestendamount();
    },
    "form.recedate"() {
      this.computeInterestendamount();
    },
    "form.rangedays"() {
      this.computeInterestendamount();
    },
    "form.buyinterest"() {
      this.form.buyamount = this.form.amount - this.form.buyinterest;
    },
    "form.saleinterest"() {
      this.form.saleamount = this.form.amount - this.form.saleinterest;
    },
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    getAccount() {
      this.$API.basedata.subject.list
        .get({ filter: [{ parent_id: { value: [1002, 1012], type: "in" } }] })
        .then(res => {
          this.accountList = res.data;
        });
    },
    selectCustomer(data) {
      this.billtradeList = data?.billtrade ?? [];
    },

    computeInterestendamount() {
      var vrate = this.form.rate;
      var vamt = this.form.amount;

      this.form.leftamount = this.form.amount;
      // 收到日期和到期日期
      var vudiff = this.$TOOL.datediff(this.form.duedate, this.form.recedate);

      var vdays = vudiff + this.form.rangedays;
      this.form.interestdays = vdays;

      var interest = (vamt * vrate * vdays) / 30000;
      this.form.interestamount = interest;
      this.form.discountamount = vamt - interest;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.finance.acceptance_rec.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    getDataInfo(id) {
      this.$API.finance.acceptance_rec.info.get({ id: id }).then(res => {
        if (res.code == 200) {
          res.data.customer && Object.assign(this.form, this.$TOOL.removeNull(res.data));
        }
      });
    },
    //表单注入数据
    setData(data) {
      this.selectCustomer(data.customer);
      Object.assign(this.form, this.$TOOL.removeNull(data));
      // this.getDataInfo(data.id);
    },
  },
};
</script>
<style></style>
